package com.goscom.app.deeset;

import com.goscom.app.database.DBAdapter;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class Section extends Activity {

	private String strMerId = "";
	private String strAct = "";
	private DBAdapter mDB;
	private EditText mEdtAns1, mEdtAns2, mEdtAns3, mEdtAns4, mEdtAns5,
			mEdtAns6, mEdtAns7, mEdtAns8, mEdtAns9, mEdtAns10, mEdtAns11,
			mEdtAns12, mEdtAns13;
	private TextView txtTit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mDB = new DBAdapter(Section.this);
		mDB.open();

		getMerId();
	}

	public void getMerId() {
		try {
			strMerId = getIntent().getStringExtra("merid");
			strAct = getIntent().getStringExtra("activity");
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (strMerId != null) {
			ContentValues content = new ContentValues();
			content = mDB.queryDataByMerAddOther("MEDICALHISTORY", strMerId,
					strAct);
			if (strAct.equals("sectiona")) {
				setContentView(R.layout.sectiona);
				txtTit = (TextView) findViewById(R.id.txtSecTit);
				txtTit.setText("SECTION A");
				initialSecA();
				loadDataSecA(content);
			}
			if (strAct.equals("sectionb")) {
				setContentView(R.layout.sectionb);
				txtTit = (TextView) findViewById(R.id.txtSecTit);
				txtTit.setText("SECTION B");
				initialSecB();
				loadDataSecB(content);
			}
			if (strAct.equals("sectionc")) {
				setContentView(R.layout.sectionc);
				txtTit = (TextView) findViewById(R.id.txtSecTit);
				txtTit.setText("SECTION C");
				initialSecC();
				loadDataSecC(content);
			}
			if (strAct.equals("sectiond")) {
				setContentView(R.layout.sectiond);
				txtTit = (TextView) findViewById(R.id.txtSecTit);
				txtTit.setText("SECTION D");
				initialSecD();
				loadDataSecD(content);
			}

		}
	}

	public void initialSecA() {
		mEdtAns1 = (EditText) findViewById(R.id.edtSectionAns1);
		mEdtAns1.setText("No");
		mEdtAns2 = (EditText) findViewById(R.id.edtSectionAns2);
		mEdtAns2.setText("No");
		mEdtAns3 = (EditText) findViewById(R.id.edtSectionAns3);
		mEdtAns3.setText("No");
		mEdtAns4 = (EditText) findViewById(R.id.edtSectionAns4);
		mEdtAns4.setText("No");
		mEdtAns5 = (EditText) findViewById(R.id.edtSectionAns5);
		mEdtAns5.setText("No");
		mEdtAns6 = (EditText) findViewById(R.id.edtSectionAns6);
		mEdtAns6.setText("No");
		mEdtAns7 = (EditText) findViewById(R.id.edtSectionAns7);
		mEdtAns7.setText("No");
		mEdtAns8 = (EditText) findViewById(R.id.edtSectionAns8);
		mEdtAns8.setText("No");
		mEdtAns9 = (EditText) findViewById(R.id.edtSectionAns9);
		mEdtAns9.setText("No");
		mEdtAns10 = (EditText) findViewById(R.id.edtSectionAns10);
		mEdtAns10.setText("No");
	}

	public void loadDataSecA(ContentValues content) {
		if (content != null) {
			mEdtAns1.setText(content.getAsString("ans1"));
			mEdtAns2.setText(content.getAsString("ans2"));
			mEdtAns3.setText(content.getAsString("ans3"));
			mEdtAns4.setText(content.getAsString("ans4"));
			mEdtAns5.setText(content.getAsString("ans5"));
			mEdtAns6.setText(content.getAsString("ans6"));
			mEdtAns7.setText(content.getAsString("ans7"));
			mEdtAns8.setText(content.getAsString("ans8"));
			mEdtAns9.setText(content.getAsString("ans9"));
			mEdtAns10.setText(content.getAsString("ans10"));
		}
	}

	public void initialSecB() {
		mEdtAns1 = (EditText) findViewById(R.id.edtSectionAns1);
		mEdtAns1.setText("No");
		mEdtAns2 = (EditText) findViewById(R.id.edtSectionAns2);
		mEdtAns2.setText("No");
		mEdtAns3 = (EditText) findViewById(R.id.edtSectionAns3);
		mEdtAns3.setText("No");
		mEdtAns4 = (EditText) findViewById(R.id.edtSectionAns4);
		mEdtAns4.setText("No");
	}

	public void loadDataSecB(ContentValues content) {
		if (content != null) {
			mEdtAns1.setText(content.getAsString("ans1"));
			mEdtAns2.setText(content.getAsString("ans2"));
			mEdtAns3.setText(content.getAsString("ans3"));
			mEdtAns4.setText(content.getAsString("ans4"));
		}
	}

	public void initialSecC() {
		mEdtAns1 = (EditText) findViewById(R.id.edtSectionAns1);
		mEdtAns1.setText("No");
		mEdtAns2 = (EditText) findViewById(R.id.edtSectionAns2);
		mEdtAns2.setText("No");
		mEdtAns3 = (EditText) findViewById(R.id.edtSectionAns3);
		mEdtAns3.setText("No");
		mEdtAns4 = (EditText) findViewById(R.id.edtSectionAns4);
		mEdtAns4.setText("No");
		mEdtAns5 = (EditText) findViewById(R.id.edtSectionAns5);
		mEdtAns5.setText("No");
		mEdtAns6 = (EditText) findViewById(R.id.edtSectionAns6);
		mEdtAns6.setText("No");
		mEdtAns7 = (EditText) findViewById(R.id.edtSectionAns7);
		mEdtAns7.setText("No");
		mEdtAns8 = (EditText) findViewById(R.id.edtSectionAns8);
		mEdtAns8.setText("No");
		mEdtAns9 = (EditText) findViewById(R.id.edtSectionAns9);
		mEdtAns9.setText("No");
		mEdtAns10 = (EditText) findViewById(R.id.edtSectionAns10);
		mEdtAns10.setText("No");
		mEdtAns11 = (EditText) findViewById(R.id.edtSectionAns11);
		mEdtAns11.setText("No");
		mEdtAns12 = (EditText) findViewById(R.id.edtSectionAns12);
		mEdtAns12.setText("No");
		mEdtAns13 = (EditText) findViewById(R.id.edtSectionAns13);
		mEdtAns13.setText("No");
	}

	public void loadDataSecC(ContentValues content) {
		if (content != null) {
			mEdtAns1.setText(content.getAsString("ans1"));
			mEdtAns2.setText(content.getAsString("ans2"));
			mEdtAns3.setText(content.getAsString("ans3"));
			mEdtAns4.setText(content.getAsString("ans4"));
			mEdtAns5.setText(content.getAsString("ans5"));
			mEdtAns6.setText(content.getAsString("ans6"));
			mEdtAns7.setText(content.getAsString("ans7"));
			mEdtAns8.setText(content.getAsString("ans8"));
			mEdtAns9.setText(content.getAsString("ans9"));
			mEdtAns10.setText(content.getAsString("ans10"));
			mEdtAns11.setText(content.getAsString("ans11"));
			mEdtAns12.setText(content.getAsString("ans12"));
			mEdtAns13.setText(content.getAsString("ans13"));
		}
	}

	public void initialSecD() {
		mEdtAns1 = (EditText) findViewById(R.id.edtSectionAns1);
		mEdtAns1.setText("No");
		mEdtAns2 = (EditText) findViewById(R.id.edtSectionAns2);
		mEdtAns2.setText("No");
		mEdtAns3 = (EditText) findViewById(R.id.edtSectionAns3);
		mEdtAns3.setText("No");
		mEdtAns4 = (EditText) findViewById(R.id.edtSectionAns4);
		mEdtAns4.setText("No");
		mEdtAns5 = (EditText) findViewById(R.id.edtSectionAns5);
		mEdtAns5.setText("No");
		mEdtAns6 = (EditText) findViewById(R.id.edtSectionAns6);
		mEdtAns6.setText("No");
	}

	public void loadDataSecD(ContentValues content) {
		if (content != null) {
			mEdtAns1.setText(content.getAsString("ans1"));
			mEdtAns2.setText(content.getAsString("ans2"));
			mEdtAns3.setText(content.getAsString("ans3"));
			mEdtAns4.setText(content.getAsString("ans4"));
			mEdtAns5.setText(content.getAsString("ans5"));
			mEdtAns6.setText(content.getAsString("ans6"));
		}
	}

	public void saveAnswer(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(Section.this, MedicalHistory.class).putExtra(
				"merid", strMerId));
	}

	public void exit(View v) {
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(Section.this, MedicalHistory.class).putExtra(
				"merid", strMerId));
	}

	public void saveDB() {
		if (mDB.queryMerchandiserAddOther("MEDICALHISTORY", strMerId, strAct) == 0) {
			insertDB();
		} else {
			mDB.deleteDataAddOther("MEDICALHISTORY", strMerId, strAct);
			insertDB();
		}
	}

	public void insertDB() {
		ContentValues content = new ContentValues();
		content.put("merid", strMerId);
		content.put("activity", strAct);
		if (strAct.equals("sectiona")) {
			insertSectionA(content);
		}
		if (strAct.equals("sectionb")) {
			insertSectionB(content);
		}
		if (strAct.equals("sectionc")) {
			insertSectionC(content);
		}
		if (strAct.equals("sectiond")) {
			insertSectionD(content);
		}
		mDB.insertData("MEDICALHISTORY", content);
	}

	public void insertSectionA(ContentValues content) {
		content.put("ans1", mEdtAns1.getText().toString());
		content.put("ans2", mEdtAns2.getText().toString());
		content.put("ans3", mEdtAns3.getText().toString());
		content.put("ans4", mEdtAns4.getText().toString());
		content.put("ans5", mEdtAns5.getText().toString());
		content.put("ans6", mEdtAns6.getText().toString());
		content.put("ans7", mEdtAns7.getText().toString());
		content.put("ans8", mEdtAns8.getText().toString());
		content.put("ans9", mEdtAns9.getText().toString());
		content.put("ans10", mEdtAns10.getText().toString());
		content.put("ans11", "No");
		content.put("ans12", "No");
		content.put("ans13", "No");
	}
	
	public void insertSectionB(ContentValues content) {
		content.put("ans1", mEdtAns1.getText().toString());
		content.put("ans2", mEdtAns2.getText().toString());
		content.put("ans3", mEdtAns3.getText().toString());
		content.put("ans4", mEdtAns4.getText().toString());
		content.put("ans5", "No");
		content.put("ans6", "No");
		content.put("ans7", "No");
		content.put("ans8", "No");
		content.put("ans9", "No");
		content.put("ans10", "No");
		content.put("ans11", "No");
		content.put("ans12", "No");
		content.put("ans13", "No");
	}
	
	public void insertSectionC(ContentValues content) {
		content.put("ans1", mEdtAns1.getText().toString());
		content.put("ans2", mEdtAns2.getText().toString());
		content.put("ans3", mEdtAns3.getText().toString());
		content.put("ans4", mEdtAns4.getText().toString());
		content.put("ans5", mEdtAns5.getText().toString());
		content.put("ans6", mEdtAns6.getText().toString());
		content.put("ans7", mEdtAns7.getText().toString());
		content.put("ans8", mEdtAns8.getText().toString());
		content.put("ans9", mEdtAns9.getText().toString());
		content.put("ans10", mEdtAns10.getText().toString());
		content.put("ans11", mEdtAns11.getText().toString());
		content.put("ans12", mEdtAns12.getText().toString());
		content.put("ans13", mEdtAns13.getText().toString());
	}
	
	public void insertSectionD(ContentValues content) {
		content.put("ans1", mEdtAns1.getText().toString());
		content.put("ans2", mEdtAns2.getText().toString());
		content.put("ans3", mEdtAns3.getText().toString());
		content.put("ans4", mEdtAns4.getText().toString());
		content.put("ans5", mEdtAns5.getText().toString());
		content.put("ans6", mEdtAns6.getText().toString());
		content.put("ans7", "No");
		content.put("ans8", "No");
		content.put("ans9", "No");
		content.put("ans10", "No");
		content.put("ans11", "No");
		content.put("ans12", "No");
		content.put("ans13", "No");
	}
	
	@Override
	protected void onRestart() {
		mDB.open();
		super.onRestart();
	}

	@Override
	protected void onDestroy() {
		if (mDB != null) {
			mDB.close();
		}
		super.onDestroy();
	}
}
